package org.example;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};

public class Test1 {
    //leetcode 429 N叉树的层序遍历 https://leetcode.cn/problems/n-ary-tree-level-order-traversal/description/
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> ret = new ArrayList<>();
        if (root == null) return ret;
        Deque<Node> deque = new ArrayDeque<>();
        deque.add(root);
        while (!deque.isEmpty()) {
            int count = deque.size();
            List<Integer> list = new ArrayList<>();
            while (count > 0) {
                Node tmp = deque.poll();
                if (!tmp.children.isEmpty()) {
                    for (Node node : tmp.children) {
                        deque.add(node);
                    }
                }
                list.add(tmp.val);
                count--;
            }
            ret.add(list);
        }
        return ret;
    }
}
